home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / elv18src.zip / vi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-13  |  21.8 KB  |  876 lines

  1. /* vi.c */
  2.  
  3. /* Author:
  4.  *    Steve Kirkendall
  5.  *    Beaverton, OR 97005
  6.  *    kirkenda@cs.pdx.edu
  7.  */
  8.  
  9.  
  10. #include "config.h"
  11. #include "ctype.h"
  12. #include "vi.h"
  13.  
  14.  
  15.  
  16. /* This array describes what each key does */
  17. #define NO_FUNC        (MARK (*)())0
  18.  
  19. #define NO_ARGS        0
  20. #define CURSOR        1
  21. #define CURSOR_CNT_KEY    2
  22. #define CURSOR_MOVED    3
  23. #define CURSOR_EOL    4
  24. #define ZERO        5
  25. #define DIGIT        6
  26. #define CURSOR_TEXT    7
  27. #define KEYWORD        8
  28. #define CURSOR_KEY    9
  29. #define ARGSMASK    0x0f
  30. #define    C_C_K_REP1    (CURSOR_CNT_KEY | 0x10)
  31. #define C_C_K_CUT    (CURSOR_CNT_KEY | 0x20)
  32. #define C_K_CUT        (CURSOR_KEY | 0x20)
  33. #define C_C_K_MARK    (CURSOR_CNT_KEY | 0x30)
  34. #define C_C_K_CHAR    (CURSOR_CNT_KEY | 0x40)
  35. #ifndef NO_SHOWMODE
  36. static int keymodes[] = {0, WHEN_REP1, WHEN_CUT, WHEN_MARK, WHEN_CHAR};
  37. # define KEYMODE(args) (keymodes[(args) >> 4])
  38. #else
  39. # define KEYMODE(args) 0
  40. #endif
  41.  
  42. static struct keystru
  43. {
  44.     MARK    (*func)();    /* the function to run */
  45.     uchar    args;        /* description of the args needed */
  46. #ifndef NO_VISIBLE
  47.     short    flags;
  48. #else
  49.     uchar    flags;        /* other stuff */
  50. #endif
  51. }
  52.     vikeys[] =
  53. {
  54. /* NUL not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  55. #ifndef NO_EXTENSIONS
  56. /* ^A  find cursor word */    {m_wsrch,    KEYWORD,    MVMT|NREL|VIZ},
  57. #else
  58. /* ^A  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  59. #endif
  60. /* ^B  page backward    */    {m_scroll,    CURSOR,        FRNT|VIZ},
  61. /* ^C  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  62. /* ^D  scroll dn 1/2page*/    {m_scroll,    CURSOR,        NCOL|VIZ},
  63. /* ^E  scroll up    */    {m_scroll,    CURSOR,        NCOL|VIZ},
  64. /* ^F  page forward    */    {m_scroll,    CURSOR,        FRNT|VIZ},
  65. /* ^G  show file status    */    {v_status,    NO_ARGS,     NO_FLAGS},
  66. /* ^H  move left, like h*/    {m_left,    CURSOR,        MVMT|VIZ},
  67. /* ^I  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  68. /* ^J  move down    */    {m_updnto,    CURSOR,        MVMT|LNMD|VIZ|INCL},
  69. /* ^K  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  70. /* ^L  redraw screen    */    {v_redraw,    NO_ARGS,    NO_FLAGS|VIZ},
  71. /* ^M  mv front next ln */    {m_updnto,    CURSOR,        MVMT|FRNT|LNMD|VIZ|INCL},
  72. /* ^N  move down    */    {m_updnto,    CURSOR,        MVMT|LNMD|VIZ|INCL|NCOL},
  73. /* ^O  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  74. /* ^P  move up        */    {m_updnto,    CURSOR,        MVMT|LNMD|VIZ|INCL|NCOL},
  75. /* ^Q  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  76. /* ^R  redraw screen    */    {v_redraw,    NO_ARGS,    NO_FLAGS|VIZ},
  77. /* ^S  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  78. #ifndef NO_TAGSTACK
  79. /* ^T  pop tagstack    */    {v_pop,        CURSOR,        NO_FLAGS},
  80. #else
  81. /* ^T  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  82. #endif
  83. /* ^U  scroll up 1/2page*/    {m_scroll,    CURSOR,        NCOL|VIZ},
  84. /* ^V  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  85. /* ^W  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  86. /* ^X  move to phys col    */    {m_tocol,    CURSOR,        MVMT|NREL|VIZ},
  87. /* ^Y  scroll down    */    {m_scroll,    CURSOR,        NCOL|VIZ},
  88. #ifdef SIGTSTP
  89. /* ^Z  suspend elvis    */    {v_suspend,    NO_ARGS,    NO_FLAGS},
  90. #else
  91. /* ^Z  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  92. #endif
  93. /* ESC not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  94. /* ^\  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  95. /* ^]  keyword is tag    */    {v_tag,        KEYWORD,    NO_FLAGS},
  96. /* ^^  previous file    */    {v_switch,    CURSOR,        FRNT},
  97. /* ^_  move to row    */    {m_row,        CURSOR,        MVMT|NCOL|INCL|VIZ},
  98. /* SPC move right,like l*/    {m_right,    CURSOR,        MVMT|INCL|VIZ},
  99. /*  !  run thru filter    */    {v_filter,    CURSOR_MOVED,    FRNT|LNMD|INCL|VIZ},
  100. /*  "  select cut buffer*/    {v_selcut,    C_K_CUT,    PTMV|VIZ},
  101. #ifndef NO_EXTENSIONS
  102. /*  #  increment number    */    {v_increment,    KEYWORD,    SDOT},
  103. #else
  104. /*  #  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  105. #endif
  106. /*  $  move to rear    */    {m_rear,    CURSOR,        MVMT|INCL|VIZ},
  107. /*  %  move to match    */    {m_match,    CURSOR,        MVMT|INCL|VIZ},
  108. /*  &  repeat subst    */    {v_again,    CURSOR_MOVED,    SDOT|NCOL|LNMD|INCL},
  109. /*  '  move to a mark    */    {m_tomark,    C_C_K_MARK,    MVMT|FRNT|NREL|LNMD|INCL|VIZ},
  110. #ifndef NO_SENTENCE
  111. /*  (  mv back sentence    */    {m_sentence,    CURSOR,        MVMT|VIZ},
  112. /*  )  mv fwd sentence    */    {m_sentence,    CURSOR,        MVMT|VIZ},
  113. #else
  114. /*  (  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  115. /*  )  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  116. #endif
  117. #ifndef NO_ERRLIST
  118. /*  *  errlist        */    {v_errlist,    CURSOR,        FRNT|NREL},
  119. #else
  120. /*  *  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  121. #endif
  122. /*  +  mv front next ln */    {m_updnto,    CURSOR,        MVMT|FRNT|LNMD|VIZ|INCL},
  123. #ifndef NO_CHARSEARCH
  124. /*  ,  reverse [fFtT] cmd*/    {m__ch,        CURSOR,        MVMT|INCL|VIZ},
  125. #else
  126. /*  ,  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  127. #endif
  128. /*  -  mv front prev ln    */    {m_updnto,    CURSOR,        MVMT|FRNT|LNMD|VIZ|INCL},
  129. /*  .  special...    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  130. /*  /  forward search    */    {m_fsrch,    CURSOR_TEXT,    MVMT|NREL|VIZ},
  131. /*  0  part of count?    */    {NO_FUNC,    ZERO,        MVMT|PTMV|VIZ},
  132. /*  1  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  133. /*  2  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  134. /*  3  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  135. /*  4  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  136. /*  5  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  137. /*  6  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  138. /*  7  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  139. /*  8  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  140. /*  9  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  141. /*  :  run single EX cmd*/    {v_1ex,        CURSOR_TEXT,    NO_FLAGS|VIZ},
  142. #ifndef NO_CHARSEARCH
  143. /*  ;  repeat [fFtT] cmd*/    {m__ch,        CURSOR,        MVMT|INCL|VIZ},
  144. #else
  145. /*  ;  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS|VIZ},
  146. #endif
  147. /*  <  shift text left    */    {v_lshift,    CURSOR_MOVED,    SDOT|FRNT|LNMD|INCL|VIZ},
  148. /*  =  preset filter    */    {v_reformat,    CURSOR_MOVED,    SDOT|FRNT|LNMD|INCL|VIZ},
  149. /*  >  shift text right    */    {v_rshift,    CURSOR_MOVED,    SDOT|FRNT|LNMD|INCL|VIZ},
  150. /*  ?  backward search    */    {m_bsrch,    CURSOR_TEXT,    MVMT|NREL|VIZ},
  151. #ifndef NO_AT
  152. /*  @  execute a cutbuf */    {v_at,        C_C_K_CUT,    NO_FLAGS},
  153. #else
  154. /*  @  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  155. #endif
  156. /*  A  append at EOL    */    {v_insert,    CURSOR,        SDOT},
  157. /*  B  move back Word    */    {m_bword,    CURSOR,        MVMT|VIZ},
  158. /*  C  change to EOL    */    {v_change,    CURSOR_EOL,    SDOT},
  159. /*  D  delete to EOL    */    {v_delete,    CURSOR_EOL,    SDOT},
  160. /*  E  move end of Word    */    {m_eword,    CURSOR,        MVMT|INCL|VIZ},
  161. #ifndef NO_CHARSEARCH
  162. /*  F  move bk to char    */    {m_Fch,        C_C_K_CHAR,    MVMT|INCL|VIZ},
  163. #else
  164. /*  F  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  165. #endif
  166. /*  G  move to line #    */    {m_updnto,    CURSOR,        MVMT|NREL|LNMD|FRNT|INCL|VIZ},
  167. /*  H  move to row    */    {m_row,        CURSOR,        MVMT|LNMD|FRNT|VIZ|INCL},
  168. /*  I  insert at front    */    {v_insert,    CURSOR,        SDOT},
  169. /*  J  join lines    */    {v_join,    CURSOR,        SDOT},
  170. #ifndef NO_EXTENSIONS
  171. /*  K  look up keyword    */    {v_keyword,    KEYWORD,    NO_FLAGS},
  172. #else
  173. /*  K  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  174. #endif
  175. /*  L  move to last row    */    {m_row,        CURSOR,        MVMT|LNMD|FRNT|VIZ|INCL},
  176. /*  M  move to mid row    */    {m_row,        CURSOR,        MVMT|LNMD|FRNT|VIZ|INCL},
  177. /*  N  reverse prev srch*/    {m_nsrch,    CURSOR,        MVMT|NREL|VIZ},
  178. /*  O  insert above line*/    {v_insert,    CURSOR,        SDOT},
  179. /*  P  paste before    */    {v_paste,    CURSOR,        SDOT},
  180. /*  Q  quit to EX mode    */    {v_quit,    NO_ARGS,    NO_FLAGS},
  181. /*  R  overtype        */    {v_overtype,    CURSOR,        SDOT},
  182. /*  S  change line    */    {v_change,    CURSOR_MOVED,    SDOT},
  183. #ifndef NO_CHARSEARCH
  184. /*  T  move bk to char    */    {m_Tch,        C_C_K_CHAR,    MVMT|INCL|VIZ},
  185. #else
  186. /*  T  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  187. #endif
  188. /*  U  undo whole line    */    {v_undoline,    CURSOR,        FRNT},
  189. #ifndef NO_VISIBLE
  190. /*  V  start visible    */    {v_start,    CURSOR,        INCL|LNMD|VIZ},
  191. #else
  192. /*  V  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  193. #endif
  194. /*  W  move forward Word*/    {m_fword,    CURSOR,        MVMT|INCL|NWRP|VIZ},
  195. /*  X  delete to left    */    {v_xchar,    CURSOR,        SDOT},
  196. /*  Y  yank text    */    {v_yank,    CURSOR_MOVED,    NCOL},
  197. /*  Z  save file & exit    */    {v_xit,        CURSOR_CNT_KEY,    NO_FLAGS},
  198. /*  [  move back section*/    {m_paragraph,    CURSOR,        MVMT|LNMD|NREL|VIZ},
  199. #ifndef NO_POPUP
  200. /*  \  pop-up menu    */    {v_popup,    CURSOR_MOVED,    VIZ},
  201. #else
  202. /*  \  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  203. #endif
  204. /*  ]  move fwd section */    {m_paragraph,    CURSOR,        MVMT|LNMD|NREL|VIZ},
  205. /*  ^  move to front    */    {m_front,    CURSOR,        MVMT|VIZ},
  206. /*  _  current line    */    {m_updnto,    CURSOR,        MVMT|LNMD|FRNT|INCL},
  207. /*  `  move to mark    */    {m_tomark,    C_C_K_MARK,    MVMT|NREL|VIZ},
  208. /*  a  append at cursor    */    {v_insert,    CURSOR,        SDOT},
  209. /*  b  move back word    */    {m_bword,    CURSOR,        MVMT|VIZ},
  210. /*  c  change text    */    {v_change,    CURSOR_MOVED,    SDOT|VIZ},
  211. /*  d  delete op    */    {v_delete,    CURSOR_MOVED,    SDOT|VIZ},
  212. /*  e  move end word    */    {m_eword,    CURSOR,        MVMT|INCL|VIZ},
  213. #ifndef NO_CHARSEARCH
  214. /*  f  move fwd for char*/    {m_fch,        C_C_K_CHAR,    MVMT|INCL|VIZ},
  215. #else
  216. /*  f  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  217. #endif
  218. /*  g  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  219. /*  h  move left    */    {m_left,    CURSOR,        MVMT|VIZ},
  220. /*  i  insert at cursor    */    {v_insert,    CURSOR,        SDOT},
  221. /*  j  move down    */    {m_updnto,    CURSOR,        MVMT|NCOL|LNMD|VIZ|INCL},
  222. /*  k  move up        */    {m_updnto,    CURSOR,        MVMT|NCOL|LNMD|VIZ|INCL},
  223. /*  l  move right    */    {m_right,    CURSOR,        MVMT|INCL|VIZ},
  224. /*  m  define a mark    */    {v_mark,    C_C_K_MARK,    NO_FLAGS},
  225. /*  n  repeat prev srch    */    {m_nsrch,    CURSOR,     MVMT|NREL|VIZ},
  226. /*  o  insert below line*/    {v_insert,    CURSOR,        SDOT},
  227. /*  p  paste after    */    {v_paste,    CURSOR,        SDOT},
  228. /*  q  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  229. /*  r  replace chars    */    {v_replace,    C_C_K_REP1,    SDOT},
  230. /*  s  subst N chars    */    {v_subst,    CURSOR,        SDOT},
  231. #ifndef NO_CHARSEARCH
  232. /*  t  move fwd to char    */    {m_tch,        C_C_K_CHAR,    MVMT|INCL|VIZ},
  233. #else
  234. /*  t  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  235. #endif
  236. /*  u  undo        */    {v_undo,    CURSOR,        NO_FLAGS},
  237. #ifndef NO_VISIBLE
  238. /*  v  start visible    */    {v_start,    CURSOR,        INCL|VIZ},
  239. #else
  240. /*  v  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  241. #endif
  242. /*  w  move fwd word    */    {m_fword,    CURSOR,        MVMT|INCL|NWRP|VIZ},
  243. /*  x  delete character    */    {v_xchar,    CURSOR,        SDOT},
  244. /*  y  yank text    */    {v_yank,    CURSOR_MOVED,    NCOL|VIZ},
  245. /*  z  adjust scrn row    */    {m_z,         CURSOR_CNT_KEY,    NCOL|VIZ},
  246. /*  {  back paragraph    */    {m_paragraph,    CURSOR,        MVMT|VIZ},
  247. /*  |  move to column    */    {m_tocol,    CURSOR,        MVMT|NREL|VIZ},
  248. /*  }  fwd paragraph    */    {m_paragraph,    CURSOR,        MVMT|VIZ},
  249. /*  ~  upper/lowercase    */    {v_ulcase,    CURSOR,        SDOT},
  250. /* DEL not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS}
  251. };
  252.  
  253.  
  254.  
  255. void vi()
  256. {
  257.     REG int            key;    /* keystroke from user */
  258.     long            count;    /* numeric argument to some functions */
  259.     REG struct keystru    *keyptr;/* pointer to vikeys[] element */
  260.     MARK            tcurs;    /* temporary cursor */
  261.     int            prevkey;/* previous key, if d/c/y/</>/! */
  262.     MARK            range;    /* start of range for d/c/y/</>/! */
  263.     char            text[132];
  264.     int            dotkey;    /* last "key" of a change */
  265.     int            dotpkey;/* last "prevkey" of a change */
  266.     int            dotkey2;/* last extra "getkey()" of a change */
  267.     int            dotcnt;    /* last "count" of a change */
  268.     int            firstkey;
  269.     REG int            i;
  270.  
  271.     /* tell the redraw() function to start from scratch */
  272.     redraw(MARK_UNSET, FALSE);
  273.  
  274. #ifdef lint
  275.     /* lint says that "range" might be used before it is set.  This
  276.      * can't really happen due to the way "range" and "prevkey" are used,
  277.      * but lint doesn't know that.  This line is here ONLY to keep lint
  278.      * happy.
  279.      */
  280.     range = 0L;
  281. #endif
  282.  
  283.     /* safeguard against '.' with no previous command */
  284.     dotkey = dotpkey = dotkey2 = dotcnt = 0;
  285.  
  286.     /* go immediately into insert mode, if ":set inputmode" */
  287.     firstkey = 0;
  288. #ifndef NO_EXTENSIONS
  289.     if (*o_inputmode)
  290.     {
  291.         firstkey = 'i';
  292.     }
  293. #endif
  294.  
  295.     /* Repeatedly handle VI commands */
  296.     for (count = 0, prevkey = '\0'; mode == MODE_VI; )
  297.     {
  298.         /* if we've moved off the undoable line, then we can't undo it at all */
  299.         if (markline(cursor) != U_line)
  300.         {
  301.             U_line = 0L;
  302.         }
  303.  
  304.         /* report any changes from the previous command */
  305.         if (rptlines >= *o_report)
  306.         {
  307.             redraw(cursor, FALSE);
  308.             msg("%ld line%s %s", rptlines, (rptlines==1?"":"s"), rptlabel);
  309.         }
  310.         rptlines = 0L;
  311.  
  312.         /* get the next command key.  It must be ASCII */
  313.         if (firstkey)
  314.         {
  315.             key = firstkey;
  316.             firstkey = 0;
  317.         }
  318.         else
  319.         {
  320.             do
  321.             {
  322.                 key = getkey(WHEN_VICMD);
  323.             } while (key < 0 || key > 127);
  324.         }
  325. #ifdef DEBUG2
  326.         debout("\nkey='%c'\n", key);
  327. #endif
  328.  
  329.         /* Convert a doubled-up operator such as "dd" into "d_" */
  330.         if (prevkey && key == prevkey)
  331.         {
  332.             key = '_';
  333.         }
  334.  
  335. #ifndef NO_VISIBLE
  336.         /* If not in the middle of a command, <Esc> cancels <v> */
  337.         if (!count && !prevkey && V_from && key == '\033')
  338.         {
  339.             key = 'v';
  340.         }
  341. #endif
  342.  
  343.         /* look up the structure describing this command */
  344.         keyptr = &vikeys[key];
  345.  
  346.         /* '&' and uppercase operators always act like doubled */
  347.         if (!prevkey && keyptr->args == CURSOR_MOVED
  348.             && (key == '&' || isupper(key)))
  349.         {
  350.             range = cursor;
  351.             prevkey = key;
  352.             key = '_';
  353.             keyptr = &vikeys[key];
  354.         }
  355.  
  356. #ifndef NO_VISIBLE
  357.         /* if we're in the middle of a v/V command, reject commands
  358.          * that aren't operators or movement commands
  359.          */
  360.         if (V_from && !(keyptr->flags & VIZ))
  361.         {
  362.             beep();
  363.             prevkey = 0;
  364.             count = 0;
  365.             continue;
  366.         }
  367. #endif
  368.  
  369.         /* if we're in the middle of a d/c/y/</>/! command, reject
  370.          * anything but movement.
  371.          */
  372.         if (prevkey && !(keyptr->flags & (MVMT|PTMV)))
  373.         {
  374.             beep();
  375.             prevkey = 0;
  376.             count = 0;
  377.             continue;
  378.         }
  379.  
  380.         /* set the "dot" variables, if we're supposed to */
  381.         if (((keyptr->flags & SDOT)
  382.             || (prevkey && vikeys[prevkey].flags & SDOT))
  383. #ifndef NO_VISIBLE
  384.             && !V_from
  385. #endif
  386.         )
  387.         {
  388.             dotkey = key;
  389.             dotpkey = prevkey;
  390.             dotkey2 = '\0';
  391.             dotcnt = count;
  392.  
  393.             /* remember the line before any changes are made */
  394.             if (U_line != markline(cursor))
  395.             {
  396.                 U_line = markline(cursor);
  397.                 strcpy(U_text, fetchline(U_line));
  398.             }
  399.         }
  400.  
  401.         /* if this is "." then set other vars from the "dot" vars */
  402.         if (key == '.')
  403.         {
  404.             key = dotkey;
  405.             keyptr = &vikeys[key];
  406.             prevkey = dotpkey;
  407.             if (prevkey)
  408.             {
  409.                 range = cursor;
  410.             }
  411.             if (count == 0)
  412.             {
  413.                 count = dotcnt;
  414.             }
  415.             else
  416.             {
  417.                 dotcnt = count;
  418.             }
  419.             doingdot = TRUE;
  420.  
  421.             /* remember the line before any changes are made */
  422.             if (U_line != markline(cursor))
  423.             {
  424.                 U_line = markline(cursor);
  425.                 strcpy(U_text, fetchline(U_line));
  426.             }
  427.         }
  428.         else
  429.         {
  430.             doingdot = FALSE;
  431.         }
  432.  
  433.         /* process the key as a command */
  434.         tcurs = cursor;
  435.         force_flags = NO_FLAGS;
  436.         switch (keyptr->args & ARGSMASK)
  437.         {
  438.           case ZERO:
  439.             if (count == 0)
  440.             {
  441.                 tcurs = cursor & ~(BLKSIZE - 1);
  442.                 break;
  443.             }
  444.             /* else fall through & treat like other digits... */
  445.  
  446.           case DIGIT:
  447.             count = count * 10 + key - '0';
  448.             break;
  449.  
  450.           case KEYWORD:
  451.             /* if not on a keyword, fail */
  452.             pfetch(markline(cursor));
  453.             key = markidx(cursor);
  454.             if (!isalnum(ptext[key]))
  455.             {
  456.                 tcurs = MARK_UNSET;
  457.                 break;
  458.             }
  459.  
  460.             /* find the start of the keyword */
  461.             while (key > 0 && isalnum(ptext[key - 1]))
  462.             {
  463.                 key--;
  464.             }
  465.             tcurs = (cursor & ~(BLKSIZE - 1)) + key;
  466.  
  467.             /* copy it into a buffer, and NUL-terminate it */
  468.             i = 0;
  469.             do
  470.             {
  471.                 text[i++] = ptext[key++];
  472.             } while (isalnum(ptext[key]));
  473.             text[i] = '\0';
  474.  
  475.             /* call the function */
  476.             tcurs = (*keyptr->func)(text, tcurs, count);
  477.             count = 0L;
  478.             break;
  479.  
  480.           case NO_ARGS:
  481.             if (keyptr->func)
  482.             {
  483.                 (*keyptr->func)();
  484.             }
  485.             else
  486.             {
  487.                 beep();
  488.             }
  489.             count = 0L;
  490.             break;
  491.     
  492.           case CURSOR:
  493.             tcurs = (*keyptr->func)(cursor, count, key, prevkey);
  494.             count = 0L;
  495.             break;
  496.  
  497.           case CURSOR_CNT_KEY:
  498.           case CURSOR_KEY:
  499.             if (doingdot)
  500.             {
  501.                 tcurs = (*keyptr->func)(cursor, count, dotkey2);
  502.             }
  503.             else
  504.             {
  505.                 /* get a key */
  506.                 i = getkey(KEYMODE(keyptr->args));
  507.                 if (i == '\033') /* ESC */
  508.                 {
  509.                     count = 0;
  510.                     tcurs = MARK_UNSET;
  511.                     break; /* exit from "case CURSOR_CNT_KEY" */
  512.                 }
  513.                 else if (i == ctrl('V'))
  514.                 {
  515.                     i = getkey(0);
  516.                 }
  517.  
  518.                 /* if part of an SDOT command, remember it */
  519.                  if (keyptr->flags & SDOT
  520.                  || (prevkey && vikeys[prevkey].flags & SDOT))
  521.                 {
  522.                     dotkey2 = i;
  523.                 }
  524.  
  525.                 /* do it */
  526.                 tcurs = (*keyptr->func)(cursor, count, i);
  527.             }
  528.             if ((keyptr->args & ARGSMASK) == CURSOR_CNT_KEY)
  529.             {
  530.                 count = 0L;
  531.             }
  532.             break;
  533.     
  534.           case CURSOR_MOVED:
  535. #ifndef NO_VISIBLE
  536.             if (V_from)
  537.             {
  538.                 range = cursor;
  539.                 tcurs = V_from;
  540.                 count = 0L;
  541.                 prevkey = key;
  542.                 key = (V_linemd ? 'V' : 'v');
  543.                 keyptr = &vikeys[key];
  544.             }
  545.             else
  546. #endif
  547.             {
  548.                 prevkey = key;
  549.                 range = cursor;
  550.                 force_flags = LNMD|INCL;
  551.             }
  552.             break;
  553.  
  554.           case CURSOR_EOL:
  555.             prevkey = key;
  556.             /* a zero-length line needs special treatment */
  557.             pfetch(markline(cursor));
  558.             if (plen == 0)
  559.             {
  560.                 /* act on a zero-length section of text */
  561.                 range = tcurs = cursor;
  562.                 key = '0';
  563.             }
  564.             else
  565.             {
  566.                 /* act like CURSOR_MOVED with '$' movement */
  567.                 range = cursor;
  568.                 tcurs = m_rear(cursor, 1L);
  569.                 key = '$';
  570.             }
  571.             count = 0L;
  572.             keyptr = &vikeys[key];
  573.             break;
  574.  
  575.           case CURSOR_TEXT:
  576.               do
  577.               {    
  578.                 text[0] = key;
  579.                 text[1] = '\0';
  580.                 if (doingdot || vgets(key, text + 1, sizeof text - 1) >= 0)
  581.                 {
  582.                     /* reassure user that <CR> was hit */
  583.                     qaddch('\r');
  584.                     refresh();
  585.  
  586.                     /* call the function with the text */
  587.                     tcurs = (*keyptr->func)(cursor, text);
  588.                 }
  589.                 else
  590.                 {
  591.                     if (exwrote || mode == MODE_COLON)
  592.                     {
  593.                         redraw(MARK_UNSET, FALSE);
  594.                     }
  595.                     mode = MODE_VI;
  596.                 }
  597.             } while (mode == MODE_COLON);
  598.             count = 0L;
  599.             break;
  600.         }
  601.  
  602.         /* if that command took us out of vi mode, then exit the loop
  603.          * NOW, without tweaking the cursor or anything.  This is very
  604.          * important when mode == MODE_QUIT.
  605.          */
  606.         if (mode != MODE_VI)
  607.         {
  608.             break;
  609.         }
  610.  
  611.         /* now move the cursor, as appropriate */
  612.         if (prevkey && ((keyptr->flags & MVMT)
  613. #ifndef NO_VISIBLE
  614.                            || V_from
  615. #endif
  616.                 ) && count == 0L)
  617.         {
  618.             /* movements used as targets are less strict */
  619.             tcurs = adjmove(cursor, tcurs, (int)(keyptr->flags | force_flags));
  620.         }
  621.         else if (keyptr->args == CURSOR_MOVED)
  622.         {
  623.             /* the < and > keys have FRNT,
  624.              * but it shouldn't be applied yet
  625.              */
  626.             tcurs = adjmove(cursor, tcurs, FINL);
  627.         }
  628.         else
  629.         {
  630.             tcurs = adjmove(cursor, tcurs, (int)(keyptr->flags | force_flags | FINL));
  631.         }
  632.  
  633.         /* was that the end of a d/c/y/</>/! command? */
  634.         if (prevkey && ((keyptr->flags & MVMT)
  635. #ifndef NO_VISIBLE
  636.                            || V_from
  637. #endif
  638.                 ) && count == 0L)
  639.         {
  640. #ifndef NO_VISIBLE
  641.             /* turn off the hilight */
  642.             V_from = 0L;
  643. #endif
  644.  
  645.             /* if the movement command failed, cancel operation */
  646.             if (tcurs == MARK_UNSET)
  647.             {
  648.                 prevkey = 0;
  649.                 count = 0;
  650.                 continue;
  651.             }
  652.  
  653.             /* make sure range=front and tcurs=rear.  Either way,
  654.              * leave cursor=range since that's where we started.
  655.              */
  656.             cursor = range;
  657.             if (tcurs < range)
  658.             {
  659.                 range = tcurs;
  660.                 tcurs = cursor;
  661.             }
  662.  
  663.             /* The 'w' and 'W' destinations should never take us
  664.              * to the front of a line.  Instead, they should take
  665.              * us only to the end of the preceding line.
  666.              */
  667.             if ((keyptr->flags & NWRP) == NWRP
  668.               && markline(range) < markline(tcurs)
  669.               && (markline(tcurs) > nlines || tcurs == m_front(tcurs, 0L)))
  670.             {
  671.                 tcurs = (tcurs & ~(BLKSIZE - 1)) - BLKSIZE;
  672.                 pfetch(markline(tcurs));
  673.                 tcurs += plen;
  674.             }
  675.  
  676.             /* adjust for line mode & inclusion of last char/line */
  677.             i = (keyptr->flags | vikeys[prevkey].flags);
  678.             switch ((i | force_flags) & (INCL|LNMD))
  679.             {
  680.               case INCL:
  681.                 tcurs++;
  682.                 break;
  683.  
  684.               case INCL|LNMD:
  685.                 tcurs += BLKSIZE;
  686.                 range &= ~(BLKSIZE - 1);
  687.                 tcurs &= ~(BLKSIZE - 1);
  688.                 break;
  689.  
  690.               case LNMD:
  691.                 tcurs--; /* if at column 0, go to prev line */
  692.                 range &= ~(BLKSIZE - 1);
  693.                 tcurs &= ~(BLKSIZE - 1);
  694.                 break;
  695.             }
  696.  
  697.             /* run the function */
  698.             tcurs = (*vikeys[prevkey].func)(range, tcurs);
  699.             if (mode == MODE_VI)
  700.             {
  701.                 (void)adjmove(cursor, cursor, FINL);
  702.                 cursor = adjmove(cursor, tcurs, (int)(vikeys[prevkey].flags | FINL));
  703.             }
  704.  
  705.             /* cleanup */
  706.             prevkey = 0;
  707.         }
  708.         else if (!prevkey)
  709.         {
  710.             if (tcurs != MARK_UNSET)
  711.                 cursor = tcurs;
  712.         }
  713.     }
  714. }
  715.  
  716. /* This function adjusts the MARK value that they return; here we make sure
  717.  * it isn't past the end of the line, and that the column hasn't been
  718.  * *accidentally* changed.
  719.  */
  720. MARK adjmove(old, new, flags)
  721.     MARK        old;    /* the cursor position before the command */
  722.     REG MARK    new;    /* the cursor position after the command */
  723.     int        flags;    /* various flags regarding cursor mvmt */
  724. {
  725.     static int    colno;    /* the column number that we want */
  726.     REG char    *text;    /* used to scan through the line's text */
  727.     REG int        i;
  728.  
  729. #ifdef DEBUG2
  730.     debout("adjmove(%ld.%d, %ld.%d, 0x%x)\n", markline(old), markidx(old), markline(new), markidx(new), flags);
  731. #endif
  732. #ifdef DEBUG
  733.     watch();
  734. #endif
  735.  
  736.     /* if the command failed, bag it! */
  737.     if (new == MARK_UNSET)
  738.     {
  739.         if (flags & FINL)
  740.         {
  741.             beep();
  742.             return old;
  743.         }
  744.         return new;
  745.     }
  746.  
  747.     /* if this is a non-relative movement, set the '' mark */
  748.     if (flags & NREL)
  749.     {
  750.         mark[26] = old;
  751.     }
  752.  
  753.     /* make sure it isn't past the end of the file */
  754.     if (markline(new) < 1)
  755.     {
  756.         new = MARK_FIRST;
  757.     }
  758.     else if (markline(new) > nlines)
  759.     {
  760.         if (!(flags & FINL))
  761.         {
  762.             return MARK_EOF;
  763.         }
  764.         new = MARK_LAST; /* !!! or MARK_EOF? */
  765.     }
  766.  
  767.     /* fetch the new line */
  768.     pfetch(markline(new));
  769.  
  770.     /* move to the front, if we're supposed to */
  771.     if (flags & FRNT)
  772.     {
  773.         new = m_front(new, 1L);
  774.     }
  775.  
  776.     /* change the column#, or change the mark to suit the column# */
  777.     if (!(flags & NCOL))
  778.     {
  779.         /* change the column# */
  780.         i = markidx(new);
  781.         if (i == BLKSIZE - 1)
  782.         {
  783.             new &= ~(BLKSIZE - 1);
  784.             if (plen > 0)
  785.             {
  786.                 new += plen;
  787.                 if (!(flags & INPM))
  788.                 {
  789.                     new--;
  790.                 }
  791.             }
  792.             colno = BLKSIZE * 8; /* one heck of a big colno */
  793.         }
  794.         else if (plen > 0)
  795.         {
  796.             if (i >= plen)
  797.             {
  798.                 new = (new & ~(BLKSIZE - 1)) + plen;
  799. #ifndef NO_EXTENSIONS
  800.                 if (!(flags & INPM))
  801. #endif
  802.                 {
  803.                     new--;
  804.                 }
  805.             }
  806.             colno = idx2col(new, ptext, FALSE);
  807.         }
  808.         else
  809.         {
  810.             new &= ~(BLKSIZE - 1);
  811.             colno = 0;
  812.         }
  813.     }
  814.     else
  815.     {
  816.         /* adjust the mark to get as close as possible to column# */
  817.         for (i = 0, text = ptext; i <= colno && *text; text++)
  818.         {
  819.             if (*text == '\t' && !*o_list)
  820.             {
  821.                 i += *o_tabstop - (i % *o_tabstop);
  822.             }
  823.             else if (UCHAR(*text) < ' ' || *text == 127)
  824.             {
  825.                 i += 2;
  826.             }
  827. #ifndef NO_CHARATTR
  828.             else if (*o_charattr && text[0] == '\\' && text[1] == 'f' && text[2])
  829.             {
  830.                 text += 2; /* plus one more in "for()" stmt */
  831.             }
  832. #endif
  833.             else
  834.             {
  835.                 i++;
  836.             }
  837.         }
  838.         
  839.         if (text > ptext && !(i <= colno && (flags & INPM)))
  840.         {
  841.             text--;
  842.         }
  843.         new = (new & ~(BLKSIZE - 1)) + (int)(text - ptext);
  844.     }
  845.  
  846.     return new;
  847. }
  848.  
  849.  
  850. #ifdef DEBUG
  851. watch()
  852. {
  853.     static wasset;
  854.  
  855.     if (*origname)
  856.     {
  857.         wasset = TRUE;
  858.     }
  859.     else if (wasset)
  860.     {
  861.         mode = MODE_EX;
  862.         msg("origname was clobbered");
  863.         endwin();
  864.         abort();
  865.     }
  866.  
  867.     if (wasset && nlines == 0)
  868.     {
  869.         mode = MODE_EX;
  870.         msg("nlines=0");
  871.         endwin();
  872.         abort();
  873.     }
  874. }
  875. #endif
  876.